package hello;

import org.hibernate.Session;
import org.hibernate.Transaction;

import persistence.HibernateUtil;

public class HelloWorld
{
	public static void M2()throws Exception
	{
		
		Session s6=HibernateUtil.getSessionFactory().openSession();
		Transaction t6=s6.beginTransaction();
		Message2 message2=new Message2("Hello World!");
		
		Address addr=new Address("first ring road","610041","cheng du");
		message2.setAddress(addr);
		
		message2.setId(4L);
		s6.saveOrUpdate(message2);
		
		//update a obj with null PK-->Exception
		//s6.update(message2);
		/*
		//even set PK,but you call save(), one record will be inserted
		message2.setId(1L);
		s6.save(message2);
		*/
		
		t6.commit();
		s6.close();
		
		/*
		Session s7=HibernateUtil.getSessionFactory().openSession();
		Transaction t7=s7.beginTransaction();
		List list=s7.createQuery("from Message2").list();
		out(list.size());
		Message2 gotMsg2=(Message2)list.get(0);
		out(gotMsg2.getAddress().getStreet()+gotMsg2.getAddress().getMsg2().getText());
		//gotMsg2.setText("set in session");
		t7.commit();
		s7.close();
		
		Session s8=HibernateUtil.getSessionFactory().openSession();
		Transaction t8=s8.beginTransaction();
		out("#########"+gotMsg2.getId());
		gotMsg2.setText("set out of orig session saveorupdate");
		s8.saveOrUpdate(gotMsg2);
		
		t8.commit();
		s8.close();
		*/
	}
	
	public static void main(String[] args)throws Exception
	{
		//M2();
		
		/*
		Session s2=HibernateUtil.getSessionFactory().openSession();
		Transaction tx2=s2.beginTransaction();
		Message msg=new Message("Hello World!");
		msg.setFlag(true);
		msg.setAddress(new Address());
		msg.setWei("sun wei");
		
		msg.setClazz("clazz".getClass());
		msg.setLocale(Locale.CHINA);
		msg.setTimezone(TimeZone.getDefault());
		Currency cny=Currency.getInstance(Locale.CHINA);
		msg.setCurrency(cny);
		msg.setMoney(new MonetaryAmount(new BigDecimal("2.35"),Currency.getInstance("CNY")));
		msg.setRating(Rating.BAD);
		
		s2.save(msg);
		out("!!!!!!! "+s2.getIdentifier(msg).getClass().getName());
		
	    s2.createQuery("update Message m set m.money.amount=2008.0").executeUpdate();
		s2.createQuery("from Message m where m.money.amount<100.0").list();
		Query q=s2.createQuery("from "+Message.class.getName()+" where rating=:rating");
		Properties params=new Properties();
		params.put("enumClassname","auction.model.Rating");
		q.setParameter("rating",Rating.OK);//Hibernate.custom(StringEnumUserType.class,params)
		q.list();
		
		
		Message2 msg2=new Message2();
		s2.save(msg2);
		
		
		
		Message msg1=(Message)s2.load(Message.class,1L);
		msg1.setMoney(new MonetaryAmount(new BigDecimal("0.99"),Currency.getInstance("USD")));
		msg1.setText("good");
		out("xxxx "+msg1.getMoney());
		
		tx2.commit();
		s2.close();
		
		
		
		Session session=HibernateUtil.getSessionFactory().openSession();
		Transaction tx=session.beginTransaction();
		Message msg1=(Message)s2.load(Message.class,1L);
		msg1.setMoney(new MonetaryAmount(new BigDecimal("0.99"),Currency.getInstance("USD")));
		out("xxxx "+msg1.getMoney());
		tx.commit();
		session.close();
		
			
		
		
		Session thirdSession=HibernateUtil.getSessionFactory().openSession();
		Transaction thirdtx=thirdSession.beginTransaction();
		message=(Message)thirdSession.load(Message.class,msgId);
		message.setText("Greeting Earthling");
		message.setWei("no mapped!"); //no mapping
		message.setNextMessage(new Message("Take me to your leader(please)"));
		thirdtx.commit();
		thirdSession.close();
		
		
		// set another next message
		Session s4=HibernateUtil.getSessionFactory().openSession();
		Transaction t4=s4.beginTransaction();
		message=(Message)s4.get(Message.class,msgId);
		message.getNextMessage().setText("www");
		t4.commit();
		s4.close();
		
		
		
		// set another next message
		Session s3=HibernateUtil.getSessionFactory().openSession();
		Transaction t3=s3.beginTransaction();
		message=(Message)s3.get(Message.class,2L);
		message.setNextMessage(new Message("update me?"));
		t3.commit();
		s3.close();
		
		
		
		//delete
		Session s1=HibernateUtil.getSessionFactory().openSession();
		Transaction tx1=s1.beginTransaction();
		message=(Message)s1.get(Message.class,1L);
		s1.delete(message);
		tx1.commit();
		s1.close();
		
		
		
		//show all
		Session newSession=HibernateUtil.getSessionFactory().openSession();
		Transaction newtx=newSession.beginTransaction();
		List messages=newSession.createQuery("from Message m order by m.text asc").list();
		System.out.println(messages.size()+" message(s) found:");
		for(Iterator it=messages.iterator();it.hasNext();)
		{
			Message loadedMsg=(Message)it.next();
			Message nextMsg=loadedMsg.getNextMessage();
			System.out.println(loadedMsg.getId()+"---"+loadedMsg.getText()+"["+(nextMsg==null?"" : nextMsg.getText())+"]");
		}
		newtx.commit();
		newSession.close();
		
		
		//show XML
		Session s5=HibernateUtil.getSessionFactory().openSession();
		Transaction t5=s5.beginTransaction();
		Session dom4jSession=s5.getSession(EntityMode.DOM4J);
		List messages=dom4jSession.createQuery("select text as a from Message m order by m.text asc").list();
		out(messages.size()+" message(s) found:");
		out(messages);
		
		OutputFormat format=OutputFormat.createPrettyPrint();
		XMLWriter writer=new XMLWriter(System.out,format);
		
		for(Iterator it=messages.iterator();it.hasNext();)
		{
			Element e=(Element)it.next();
			writer.write(e);
		}
		t5.commit();
		s5.close();
		*/
		
		//HibernateUtil.stat();
		//HibernateUtil.shutdown();
	}
	
	public static void getParent(Class clazz)
	{
		Class parent=clazz.getSuperclass();
	  if(parent !=null)
	  {
			System.out.println("###"+parent.getName());
			getParent(parent);
		}
	}
	
	static void out(Object obj)
	{
		System.out.println(obj);
	}
}
